﻿using SCM.Common;
using SCM.Common.Extensions;
using SCM.Model;
using SCM.Model.DBEntity;
using SCM.Servie.IBusiness;
using System;
using System.Collections.Generic;
using System.Linq.Expressions;

namespace SCM.Servie.Business
{
    /// <summary>
    /// 企业服务
    /// </summary>
    public class CompanyService : ICompanyService
    {
        private readonly DbContext dal = new DbContext();

        public string Add(T_Company model)
        {
            return dal.Insertable(model).ExecuteReturnEntity().ID;
        }

        public bool Delete(List<string> ids)
        {
            return dal.Updateable<T_Company>(new
            {
                DataStatus = DataStatusEnum.Deleted,
                ModifyTime = DateTime.Now,
            }).Where(p => ids.Contains(p.ID)).ExecuteCommand() > 0;
        }

        public T_Company Get(string companyName,string identityNumber, string telPhone, string email)
        {
            return dal.QueryOne<T_Company>(p => (p.CompanyName == companyName || p.IdentityNumber == identityNumber || p.TelPhone == telPhone || p.Email == email) && p.DataStatus == DataStatusEnum.Valid);
        }

        public T_Company GetById(string id)
        {
            return dal.Get<T_Company>(id);
        }

        public List<T_Company> GetList(string identityNumber)
        {
            Expression<Func<T_Company, bool>> whereExp = (a) => a.DataStatus == DataStatusEnum.Valid;
            if (!string.IsNullOrWhiteSpace(identityNumber))
            {
                whereExp = whereExp.Compose((a) => a.IdentityNumber == identityNumber, Expression.AndAlso);
            }
            return dal.Queryable<T_Company>().Where(whereExp).ToList();
        }

        public PageData<T_Company> Query(string companyName, string shortName, string identityNumber, string telPhone, PageInfo page)
        {
            int count = 0;
            Expression<Func<T_Company, bool>> whereExp = (a) => a.DataStatus == DataStatusEnum.Valid;
            if (!string.IsNullOrWhiteSpace(companyName))
            {
                whereExp = whereExp.Compose((a) => a.CompanyName.StartsWith(companyName), Expression.AndAlso);
            }
            if (!string.IsNullOrWhiteSpace(shortName))
            {
                whereExp = whereExp.Compose((a) => a.ShortName.StartsWith(shortName), Expression.AndAlso);
            }
            if (!string.IsNullOrWhiteSpace(shortName))
            {
                whereExp = whereExp.Compose((a) => a.IdentityNumber.StartsWith(identityNumber), Expression.AndAlso);
            }
            if (!string.IsNullOrWhiteSpace(telPhone))
            {
                whereExp = whereExp.Compose((a) => a.TelPhone.StartsWith(telPhone), Expression.AndAlso);
            }

            List<T_Company> list = dal.Queryable<T_Company>().Where(whereExp).OrderBy(a => a.CreateTime, SqlSugar.OrderByType.Desc).ToPageList(page.PageIndex, page.PageSize, ref count);
            page.DataCount = count;
            return new PageData<T_Company> { Data = list, Page = page };
        }

        public bool Update(T_Company model)
        {
            return dal.Modify(model);
        }

        public bool UpdateValidDate(T_Company model)
        {
            return dal.Updateable<T_Company>(p => new T_Company()
            {
                ValidDate = model.ValidDate,
                ModifyUserId = model.ModifyUserId,
                ModifyUserName = model.ModifyUserName,
            }).Where(p => p.ID == model.ID).ExecuteCommand() > 0;
        }
    }
}
